package com.sftz.modules.minapp.searchcourse.service;

import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.google.common.collect.Maps;
import com.sftz.assets.enums.CourseStateEnum;
import com.sftz.modules.minapp.searchcourse.dto.SearchCourseDto;
import com.sftz.modules.minapp.searchcourse.form.SearchCourseForm;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * 首页-课程检索
 * @author   王宏伟
 * @Date	 2018年12月11日
 */
@IocBean
public class SearchCourseService {
	private static final Log log = Logs.get();
	@Inject
	protected IDbDao dbDao;
	@Inject
	protected Dao nutDao;
	@Inject
	protected SqlManager sqlManager;

	/**
	 * 订单列表
	 * @author	王宏伟
	 * @Date	2018年12月17日
	 */
	public Map<String, Object> courseDataList(SearchCourseForm form) {
		Integer pageNumber = form.getPageNumber();
		Integer pageSize = form.getPageSize();
		Map<String, Object> map = Maps.newHashMap();
		//获取订单状态
		String searchValue = form.getSearchValue();
		List<SearchCourseDto> dataList = null;
		if (!Util.isEmpty(searchValue)) {
			int pageNum = (pageNumber - 1) * pageSize;
			Sql sql = Sqls.create(sqlManager.get("minapp_course_list"));
			sql.setParam("pageNumber", pageNum);
			sql.setParam("pageSize", pageSize);
			Cnd cnd = Cnd.NEW();
			//检索课程名称

			cnd.and("c.courseName", "like", "%" + searchValue + "%");

			cnd.and("c.courseState", "=", CourseStateEnum.PUBLISHED.intKey());
			sql.setCondition(cnd);
			dataList = DbSqlUtil.query(dbDao, SearchCourseDto.class, sql);
			//获取总条数
			Sql countSql = Sqls.create(sqlManager.get("minapp_course_list_count"));
			Cnd countCnd = Cnd.NEW();
			//检索课程名称
			countCnd.and("c.courseName", "like", "%" + searchValue + "%");
			countCnd.and("c.courseState", "=", CourseStateEnum.PUBLISHED.intKey());
			countSql.setCondition(countCnd);
			SearchCourseDto dto = DbSqlUtil.fetchEntity(dbDao, SearchCourseDto.class, countSql);
			Integer count = dto.getCount();
			map.put("dataList", dataList);
			map.put("count", count);
			map.put("pageNum", pageNum);
		} else {
			map.put("dataList", dataList);
			map.put("count", 0);
			map.put("pageNum", 0);
		}
		map.put("pageNumber", pageNumber);
		map.put("pageSize", pageSize);
		return map;
	}
}
